﻿using PC.AzureMultiApp.Core.Counters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using PC.AzureMultiApp.Core.Models;
using System.Collections.Generic;
using PC.AzureMultiApp.Storage;

namespace Core.Test
{
    
    
    /// <summary>
    ///This is a test class for CountersTest and is intended
    ///to contain all CountersTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CountersTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        [TestMethod()]
        [HostType("Moles")]
        public void ClearTest()
        {
            PC.AzureMultiApp.Storage.Moles.MCounterDataSource.Constructor = (a) => { };

            bool called = false;
            PC.AzureMultiApp.Storage.Moles.MCounterDataSource.AllInstances.Clear = (a)
            =>
            {
                called = true;
            };


            Counters.Clear();
            Assert.IsTrue(called);
        }


        [TestMethod()]
        [HostType("Moles")]
        public void GetCounters_ZeroResults_Test()
        {
            PC.AzureMultiApp.Storage.Moles.MCounterDataSource.Constructor = (a) => { };

            bool called = false;
            PC.AzureMultiApp.Storage.Moles.MCounterDataSource.AllInstances.GetCounters = (a)
                =>
            {
                called = true;
                return new List<AzureCounterEntity>();
            };

            IEnumerable<AzureCounterModel> result = Counters.GetCounters(true);
            Assert.IsTrue(result.Count() == 0);
            Assert.IsTrue(called);
        }

        [TestMethod()]
        [HostType("Moles")]
        public void GetCounters_OnlyDistincts_Test()
        {
            PC.AzureMultiApp.Storage.Moles.MCounterDataSource.Constructor = (a) => { };

            bool called = false;
            PC.AzureMultiApp.Storage.Moles.MCounterDataSource.AllInstances.GetCounters = (a)
                =>
            {
                called = true;
                return new List<AzureCounterEntity>() 
                { 
                    new AzureCounterEntity() {  CounterName = "counter", RoleInstance = "role", EventTickCount = DateTime.Now.Ticks }, 
                    new AzureCounterEntity() {  CounterName = "counter", RoleInstance = "role", EventTickCount = DateTime.Now.Ticks},
                    new AzureCounterEntity() {  CounterName = "counter", RoleInstance = "role", EventTickCount = DateTime.Now.Ticks}
                };
            };

            IEnumerable<AzureCounterModel> result = Counters.GetCounters(true);
            Assert.IsTrue(result.Count() == 1);
            Assert.IsTrue(called);
        }

        [TestMethod()]
        [HostType("Moles")]
        public void GetCountersTest()
        {
            PC.AzureMultiApp.Storage.Moles.MCounterDataSource.Constructor = (a) => { };

            bool called = false;
            PC.AzureMultiApp.Storage.Moles.MCounterDataSource.AllInstances.GetCounters = (a)
                =>
            {
                called = true;
                return new List<AzureCounterEntity>() 
                { 
                    new AzureCounterEntity() {  CounterName = "counter", RoleInstance = "role", EventTickCount = DateTime.Now.Ticks }, 
                    new AzureCounterEntity() {  CounterName = "counter", RoleInstance = "role", EventTickCount = DateTime.Now.Ticks},
                    new AzureCounterEntity() {  CounterName = "counter", RoleInstance = "role", EventTickCount = DateTime.Now.Ticks}
                };
            };

            IEnumerable<AzureCounterModel> result = Counters.GetCounters(false);
            Assert.IsTrue(result.Count() == 3);
            Assert.IsTrue(called);
        }
    }
}
